<!--
  -    Copyright (c) 2018-2025, infomng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: infomng
  -->

<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crudItem"
        v-model="form"
        :page.sync="itemPage"
        :data="tableDictItemData"
        :permission="permissionList"
        :before-open="handleBeforeOpen"
        :option="tableDictItemOption"
        @search-change="searchChange"
        @on-load="getDictItemList"
        @size-change="itemSizeChange"
        @current-change="itemCurrentChange"
        @row-update="handleItemUpdate"
        @row-save="handleItemSave"
        @row-del="rowItemDel" />
    </basic-container>
  </div>
</template>

<script>
import {
  addItemObj,
  delItemObj,
  fetchItemList,
  putItemObj,
} from '@/api/admin/dict'
import {tableDictItemOptionType} from '@/const/crud/admin/dict'
import { remote} from '@/api/admin/dict' //获取内部品类
import { mapGetters } from 'vuex'

export default {
  name: 'Dict',
  data() {
    return {
      searchForm: {},
      form: {
        dictType: undefined,
        dictId: undefined
      },
      dictType: "product_line",
      dictId: "1750723476677685250",
      dialogFormVisible: false,
      tableDictItemData: [],
      itemPage: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableDictItemOption: tableDictItemOptionType,
      internalCategory:[]
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.sys_dict_add, false),
        delBtn: this.vaildData(this.permissions.sys_dict_del, false),
        editBtn: this.vaildData(this.permissions.sys_dict_edit, false)
      }
    }
  },
  created() {
    this.getInternalCategoryData();
  },
  methods: {
    //获取内部品类
    getInternalCategoryData(){
      remote("inner_type").then(response => {
        this.internalCategory=response.data.data;
      })
    },
    //======字典表格相关=====
    getDictItemList() {
      this.dialogFormVisible = true
      this.searchForm.dictId = this.dictId;
      fetchItemList(Object.assign({
        descs:'create_time',
        current: this.itemPage.currentPage,
        size: this.itemPage.pageSize
      }, this.searchForm)).then(response => {
        this.tableDictItemData = response.data.data.records
        this.itemPage.total = response.data.data.total
      })
    },
    handleBeforeOpen(done) {
      this.form.dictType = this.dictType
      this.form.dictId = this.dictId
      done()
    },
    handleItemSave: function(row, done,loading) {
      //判断如果this.internalCategory中的label存在row.label则提示产品类型已存在，请在相应产品类型中进行添加修改
      if(this.internalCategory.some(item => item.label === row.label)){
        this.$message.warning('产品类型已存在，请在相应产品类型中进行添加修改')
        loading();
        return
      }
      row.value = row.label;
      row.itemCode = row.itemCode.toString()
      addItemObj(row).then(() => {
        this.$message.success('添加成功')
        this.getDictItemList()
        done()
      })
    },
    handleItemUpdate: function(row, index, done) {
      row.itemCode = row.itemCode.toString()
      putItemObj(row).then(() => {
        this.$message.success('修改成功')
        this.getDictItemList()
        done()
      })
    },
    itemSizeChange(pageSize) {
      this.itemPage.pageSize = pageSize
      this.getDictItemList()
    },
    itemCurrentChange(current) {
      this.itemPage.currentPage = current
      this.getDictItemList()
    },
    rowItemDel: function(row) {
      this.$confirm('是否确认删除数据为"' + row.label + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delItemObj(row.id)
      }).then(() => {
        this.getDictItemList()
        this.$message.success('删除成功')
      }).catch(function() {
      })
    },
    searchChange(form, done) {
      this.searchForm = form
      this.itemPage.currentPage = 1
      this.getDictItemList(this.itemPage, form)
      done()
    },

  }
}
</script>

<style lang="scss" scoped>
</style>

